package com.it.gw.base

import android.content.Context
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.it.gw.R

/**
 *  @Description：fragment基础类
 *  @author：weishixiong
 *  @date：2023/3/28 14:25
 *
 */
abstract class BaseFragment<T : ViewBinding> : Fragment() {
    lateinit var binding: T
    private lateinit var mActivity: AppCompatActivity


    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return getView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initObserver()
        initData()
        initListener()

    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    abstract fun initObserver()

    abstract fun initData()

    abstract fun initView()

    abstract fun initListener()

    abstract fun getView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View?

    fun setTitle(
        title: Int,
        isBack: Boolean? = false,
        ivRightRes1: Int? = null,
        tvRightRes1: Int? = null,
        backAction: (() -> Unit)? = null,
        ivRight1Action: (() -> Unit)? = null,
        tvRight1Action: (() -> Unit)? = null,
        textColorRes: Int = R.color.white,
        bgColorRes: Int = R.color.white,

        ) {
        val llTitleBg = binding.root.findViewById<LinearLayout>(R.id.ll_title_bg)
        val tvTitle = binding.root.findViewById<TextView>(R.id.tv_title)
        val ivLeft1 = binding.root.findViewById<ImageView>(R.id.iv_left1)
        val ivRight1 = binding.root.findViewById<ImageView>(R.id.iv_right1)
        val tvRight1 = binding.root.findViewById<TextView>(R.id.tv_right1)

        llTitleBg.setBackgroundColor(ContextCompat.getColor(requireActivity(), bgColorRes))
        tvTitle.setTextColor(ContextCompat.getColor(requireActivity(), textColorRes))
        tvTitle.setText(title)


        if (isBack == true) {
            ivLeft1.visibility = View.VISIBLE
            ivLeft1.imageTintList =
                ColorStateList.valueOf(ContextCompat.getColor(requireActivity(), textColorRes))
            ivLeft1.setOnClickListener {
                backAction?.invoke()
            }
        } else {
            ivLeft1.visibility = View.GONE
        }


        if (ivRightRes1 != null) {
            ivRight1.visibility = View.VISIBLE
            ivRight1.imageTintList =
                ColorStateList.valueOf(ContextCompat.getColor(requireActivity(), textColorRes))
            ivRight1.setImageResource(ivRightRes1)
            ivRight1.setOnClickListener {
                ivRight1Action?.invoke()
            }
        } else {
            ivRight1.visibility = View.GONE
        }

        if (tvRightRes1 != null) {
            tvRight1.visibility = View.VISIBLE
            tvRight1.setTextColor(ContextCompat.getColor(requireActivity(), textColorRes))
            tvRight1.setText(tvRightRes1)
            tvRight1.setOnClickListener {
                tvRight1Action?.invoke()
            }
        } else {
            tvRight1.visibility = View.GONE
        }


    }

}